#!/bin/bash
#检查cpu、内存、硬盘、网络流量的监控脚本，如果超过设置的值进行告警
 
checkPhy(){
    

    
    disk_used_per=$(df | grep -w $2| awk '{print $(NF-1)}' | awk -F'%' '{print $1}')
   
    if [ $disk_used_per -gt  $1 ];then
        echo "硬盘告警，$2硬盘使用率大于$1 %"
		echo "机房编号：${SERVER_IDC_NUM},主机：$(hostname) 硬盘告警，请及时处理"|mail -v -s "系统告警通知，请登录控制台查看" ${ADMIN_EMAIL} 
        return 1
    else 
        echo "$2硬盘使用率 $disk_used_per %,正常"
    fi

}

#checkPhy 90 /


check(){
    CPU_us=$(vmstat | awk '{print $13}' | sed -n '$p')
    CPU_sy=$(vmstat | awk '{print $14}' | sed -n '$p')
    CPU_id=$(vmstat | awk '{print $15}' | sed -n '$p')
    CPU_wa=$(vmstat | awk '{print $16}' | sed -n '$p')
    CPU_st=$(vmstat | awk '{print $17}' | sed -n '$p')
    
    CPU1=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
    sleep 5
    CPU2=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
    IDLE1=`echo $CPU1 | awk '{print $4}'`
    IDLE2=`echo $CPU2 | awk '{print $4}'`
    CPU1_TOTAL=`echo $CPU1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
    CPU2_TOTAL=`echo $CPU2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
    IDLE=`echo "$IDLE2-$IDLE1" | bc`
    CPU_TOTAL=`echo "$CPU2_TOTAL-$CPU1_TOTAL" | bc`
    #echo -e "IDLE2:$IDLE2\nIDLE1:$IDLE1\nCPU2:$CPU2_TOTAL\nCPU1:$CPU1_TOTAL"
    #echo -e        "IDLE:$IDLE\nCPU:$CPU_TOTAL"
    RATE=`echo "scale=4;($CPU_TOTAL-$IDLE)/$CPU_TOTAL*100" | bc | awk '{printf "%.2f",$1}'`
    
    echo -e "us=$CPU_us\tsy=$CPU_sy\tid=$CPU_id\twa=$CPU_wa\tst=$CPU_st"
    echo "CPU_RATE:${RATE}%"
    CPU_RATE=`echo $RATE | cut -d. -f1`
    #echo   "CPU_RATE:$CPU_RATE"
    if      [ $CPU_RATE -ge $1 ]
    then    echo "CPU Warn"
			echo "机房编号：${SERVER_IDC_NUM},主机：$(hostname) cpu使用率${RATE} %,大于90%告警，请及时处理"|mail -v -s "系统告警通知，请登录控制台查看" ${ADMIN_EMAIL} 
            ps aux | grep -v USER | sort -rn -k3 | head
            return 1
    fi
}
#cpu使用率大于90%进行报警
#check 90 

checkMem(){

    mt=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '1p'`
mu=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '2p'`
mf=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '3p'`
st=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '9p'`
su=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '10p'`
sf=`free | tr [:blank:] \\\n | grep [0-9] | sed -n '11p'`

total_size=`echo "scale=2; $mt/1024/1024" |bc`
echo "总内存：$total_size"
u=$((mu+su))
used_mem=`echo "scale=2;$u/1024/1024 "|bc`
echo "已使用内存：$used_mem"
R=`echo "scale=2;$used_mem* 100/$total_size" |bc`
echo "当前内存使用率为: $R %" 
if [ `echo "$R > $1"|bc` -eq 1 ];then
    echo "内存告警，内存使用率大于$1"
	echo "机房编号：${SERVER_IDC_NUM},主机：$(hostname)，内存使用率：$R %,内存大于90%告警，请及时处理"|mail -v -s "系统告警通知，请登录控制台查看" ${ADMIN_EMAIL} 
    return 1
else
    echo "内存使用率正常"    
fi
    
}


checkNet(){
    
   

    
    eth=$1
    
    timer=$2
    
    
    
    in_old=$(cat /proc/net/dev | grep  -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $1 }')
    
    out_old=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $9 }')
    
    echo $in_old
    echo $out_old
    

            sleep ${timer}
    
            in=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $1 }')
    
            out=$(cat /proc/net/dev | grep -w  $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $9 }')
            echo $in
            dif_in=$((in-in_old))
            dif_in=$((dif_in/timer))
            dif_in=$((dif_in/1024))
    
            dif_out=$(((out-out_old)/timer))
    
            dif_out=$((dif_out/1024))
    
            ct=$(date +"%F %H:%M:%S")
    
            echo "${ct} -- IN: ${dif_in} KByte/s     OUT: ${dif_out} KByte/s"
    
            in_old=${in}
    
            out_old=${out}
            if [ $dif_in -gt $3 ];then
                echo "网络流量告警，大于$3 kb/s"
                return 1;
            fi
 
}


main(){
#检查硬盘空间
checkPhy 90 /
#cpu使用率大于90%进行报警
check 90 
#内存使用率大于90%
checkMem 90
checkNet eth0 10 10240

}

main